""" @authors: Tobias Wilken, and Alexandro Jimenez ...

Here should be license, short description ...

"""

from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
from google.appengine.ext import db
import re
from Logic import Logic
from Tag import Tag
from Response import Response
from Connection import Connection


def OnRobotAdded(properties, context):
	""" This is invoked when the robot is added - Maybe it should say something when it is added"""
	root_wavelet = context.GetRootWavelet()
	root_wavelet.CreateBlip().GetDocument().SetText("I'm alive! But still under construction")

def OnBlibCreated(properties, context):
	""" This is invoked when a blip is submitted """
	blipId = properties['blipId'];
	blip = context.GetBlipById(blipId)
	text = blip.GetDocument().GetText()
	
	""" Here we have to:
		- check if the last character is a question mark
			- we also have to remove white spaces of line breaks
		- tokenize the string into the single words
		- ...
	"""
	responses = []
	logic = Logic()
	logic.findAnswer(text)
	if logic.answerPresent():
			newBlip = blip.CreateChild()
			newBlip.GetDocument().SetText(logic.getAnswer())

if __name__ == '__main__':
  myRobot = robot.Robot('Wave Helper', 
      image_url='http://wave-helper.appspot.com/public/icon.png',
      version='0.01',
      profile_url='http://wave-helper.appspot.com')
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlibCreated)
  myRobot.Run()
